<?php
    class Menu extends MY_Controller
    {
        public $article='';
        public function __construct()
        {
            parent::__construct();
            $this->load->model('admin/menu_model');
            $this->load->model('admin/article_model');
            $this->load->model('admin/category_article_model');
            $this->load->model('admin/category_product_model');
            $this->article=$this->article_model->getAll(1000,0,1);
        }
        public function index()
        {
            $data=$this->menu_model->getAll(0,0,-1);
            $this->load->vars('item',$data);
        }
        
        //add new article
        public function add()
        {
            $menu=$this->menu_model->getAll(0,0,1);
            $this->load->vars('menu',$menu);
            $this->load->vars('article',$this->article);
        }
        
        //edit article 
        public function edit()
        {
            $id=$this->uri->segment(4);
            $menu=$this->menu_model->getmenuById($id);
            $this->load->vars('item',$menu);
            $menu=$this->menu_model->getAll(0,0,1);
            $this->load->vars('menu',$menu);
            $this->load->vars('article',$this->article);
        }
        
        //save new article or update 
        public function save()
        {
            /*if ($this->form_validation->run('save') == FALSE)
            {
                $this->form_validation->set_error_delimiters('<div class="error" style="text-align:center;">', '</div>');
                $this->load->view('admin/form/form');
            }
            else
            { */
                $post=$this->input->post();
                $id=$post['id'];
                $name=$post['name'];
                $alias=$post['alias'];
                $link=$post['link'];
                $parent=$post['parent'];
                $position=$post['position'];
                $publish=$post['publish'];
                $order=$post['order'];
                $data=array();
                $data['menu_title']=$name;
                $data['menu_alias']=$alias;
                $data['menu_link']=$link;
                $data['menu_parent']=$parent;
                $data['menu_position']=$position;
                $data['menu_order']=$order;
                $data['menu_publish']=$publish;
                //call save
                if($this->menu_model->save($id,$data)>0)
                {
                    
                   
                } 
                 redirect(base_url()."admin/menu");
           // }
           
            
        }
        //publish article
        public function publish()
        {
            $id=$this->uri->segment(4);
            $this->menu_model->publish(1,array($id));
            redirect(base_url()."admin/menu");
        }
        //unpublish
        public function unpublish()
        {
            $id=$this->uri->segment(4);
            $this->menu_model->publish(0,array($id));
            redirect(base_url()."admin/menu");
        }
        //for actions from menu
        
        public function action()
        {
            $task=$this->input->post('task');
            $task=trim($task);
            $id=$this->input->post('id');
            switch($task)
            {
                case 'saveorder':
                $cid=$this->input->post('cid');
                $order=$this->input->post('order');
                $this->menu_model->saveorder($cid,$order);
                break;
                case 'delete':
                $this->menu_model->delete($id);
                break;
                case 'publish':
                $this->menu_model->publish(1,$id);
                break;
                case 'unpublish':
                $this->menu_model->publish(0,$id);
                break;
                
            }
            redirect(base_url()."admin/menu");
        }
        public function getlink()
        {
            $type=$this->input->post('type');
            $data="";
            switch($type)
            {
                case 1:
                $article=$this->article_model->getAll(0,0,1);
                if(!empty($article))
                {
                    foreach($article as $art)
                    {
                        echo '<option value="bai-viet/'.$art->article_alias.'">'.$art->article_title.'</option>';
                    }
                }
                else
                {
                    echo '<option value=""> Không có bài viêt </option>';
                }
                break;
                
                case 2:
                $cat_article=$this->category_article_model->getAll(0,0,1);
                if(!empty($cat_article))
                {
                    foreach($cat_article as $cat)
                    {
                        echo '<option value="chu-de-bai-viet/'.$cat->category_article_alias.'">'.$cat->category_article_title.'</option>';
                    }
                }
                else
                {
                    echo '<option value=""> Không có chủ bài viêt </option>';
                }
                break;
                
                case 3:
                $cat_product=$this->category_product_model->getAll(0,0,1);
                if(!empty($cat_product))
                {
                    foreach($cat_product as $cat_p)
                    {
                        echo '<option value="nhu-yeu-pham/'.$cat_p->category_product_alias.'">'.$cat_p->category_product_name.'</option>';
                    }
                }
                else
                {
                    echo '<option value=""> Không có nhu yếu phẩm </option>';
                }
                
                break;
                case 4:
                    echo '<option value="#">Link rỗng </option>';
                break;
                case 5:
                    echo '<option value="contact">Liên hệ</option>';
                    echo '<option value="dang-ky">Đăng ký</option>';
                break;
            }
        }
    }